<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" encoding="US-ASCII" indent="yes"
              doctype-public="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
              doctype-system="http://java.sun.com/dtd/web-app_2_3.dtd"/>

  <xsl:template match="web-app">

    <!-- p1 is the index of the last <servlet> element.  Since
         <servlet> (and pretty much everything before it) is optional,
         we include the entire set of things that may appear before
         servlet and choose the last one as the basis point.  -->
    <xsl:variable name="p1" select="count((icon|display-name|description|distributable|context-param|filter|filter-mapping|listener|servlet)[last()]/preceding-sibling::node()) + 1"/>

    <!-- p2 is the index of the last <servlet-mapping> element.  The
         logic here is similar to above. -->
    <xsl:variable name="p2" select="count((node()[position() &lt;= $p1] | servlet-mapping)[last()]/preceding-sibling::node()) + 1" />

    <xsl:copy>
      <xsl:apply-templates select="node()[position() &lt;= $p1]"/>

      <xsl:comment>BEGIN jxtc generated fragment</xsl:comment>
      <xsl:copy-of select="document('fragment')/*/servlet"/>
      <xsl:comment>END jxtc generated fragment</xsl:comment>

      <xsl:apply-templates select="node()[position() > $p1 and position() &lt;= $p2]"/>

      <xsl:comment>BEGIN jxtc generated fragment</xsl:comment>
      <xsl:copy-of select="document('fragment')/*/servlet-mapping"/>
      <xsl:comment>END jxtc generated fragment</xsl:comment>

      <xsl:apply-templates select="node()[position() > $p2]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
